package com.controller;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.entity.AreaResource;
import com.entity.StationResource;
import com.github.pagehelper.PageInfo;
import com.service.AreaResourceService;
import com.service.StationResourceService;

@Controller
@RequestMapping("/srcon")
public class StationResourceController {

	@Autowired
	StationResourceService sts;
	@Autowired
	AreaResourceService ars;
	
	@RequestMapping("/list")
	public String getAll(Integer pageNum,Model m){
		if (pageNum==null) {
			pageNum=1;
		}
		PageInfo<StationResource> pi=new PageInfo<StationResource>(sts.getAll(pageNum));
		m.addAttribute("pi", pi);
		m.addAttribute("pageNum", pageNum);
		return "/WEB-INF/jsp/show.jsp";
		
	}
	@RequestMapping("addt")
	public String addt(Model m){
		List<AreaResource> listar=ars.getAll();
		m.addAttribute("listar", listar);
		return "/WEB-INF/jsp/add.jsp";
		
	}
	@RequestMapping("add")
	public String add(Model m,StationResource sr){
		System.out.println(sr.getAr());
		System.out.println(sr.getName());
		System.out.println(sr.getSlevel());
		sr.setAr(ars.getById(sr.getAr().getId()));
		sts.add(sr);
		return "list";
		
	}
	@RequestMapping("del")
	public String del(Integer[] ck){
		for (Integer cks : ck) {
			System.out.println(cks);
			sts.del(cks);
		}
		return "list";
	}
	@RequestMapping("updatet")
	public String updatet(Model m,Integer id){
		StationResource st=sts.getById(id);
		System.out.println(st);
		
		m.addAttribute("st",st);
		List<AreaResource> listar=ars.getAll();
		m.addAttribute("listar", listar);
		return "/WEB-INF/jsp/update.jsp";
		
	}
	@RequestMapping("update")
	public String update(Model m,StationResource sr){
		System.out.println(sr.getAr());
		System.out.println(sr.getName());
		System.out.println(sr.getSlevel());
		System.out.println(ars.getById(sr.getAr().getId()));
		sr.setAr(ars.getById(sr.getAr().getId()));
		
		sts.update(sr);;
		return "list";
		
	}
}
